package com.xxzhy.mypoints;

import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private ViewPager vpFragment;
    private LinearLayout llPoints;
    private static final int LWC = LinearLayout.LayoutParams.WRAP_CONTENT;
    private int mPointDrawableResId = R.drawable.selector_bgabanner_point;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        vpFragment = findViewById(R.id.vp_fragment);
        llPoints = findViewById(R.id.ll_points);
        List<Fragment> fragments = setData();
        addPoints(fragments.size());
        vpFragment.setAdapter(new MyAdapter(getSupportFragmentManager(),fragments));
        vpFragment.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switchToPoint(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private List<Fragment> setData() {
        List<Fragment> mList = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            BaseFragment baseFragment = new BaseFragment();
            baseFragment.settype(i);
            mList.add(baseFragment);
        }
        return mList;
    }
    /**
     * 添加指示点
     */
    private void addPoints(int length) {
        llPoints.removeAllViews();
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LWC, LWC);
        lp.setMargins(10, 10, 10, 10);
        ImageView imageView;
        for (int i = 0; i < length; i++) {
            imageView = new ImageView(MainActivity.this);
            imageView.setLayoutParams(lp);
            imageView.setImageResource(mPointDrawableResId);
            llPoints.addView(imageView);
        }
        switchToPoint(0);
    }

    /**
     * 切换指示器
     *
     * @param currentPoint
     */
    private void switchToPoint(final int currentPoint) {
        for (int i = 0; i < llPoints.getChildCount(); i++) {
            llPoints.getChildAt(i).setEnabled(false);
        }
        llPoints.getChildAt(currentPoint).setEnabled(true);

    }
}
